﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using Transferomatic.Enumerators;

namespace Transferomatic.Views
{
    public partial class MainView : Form
    {
        #region Constants
        public const int COLUMN_INDEX_TYPE = 0;
        public const int COLUMN_INDEX_ACTION = 1;
        public const int COLUMN_INDEX_SOURCE = 2;
        public const int COLUMN_INDEX_DESTINATION = 3;
        public const int COLUMN_INDEX_CURRENT_FILE = 4;
        public const int COLUMN_INDEX_PROGRESS = 5;
        public const int COLUMN_INDEX_STATUS = 6;
        #endregion

        #region Members
        private MainViewStatus _Status = MainViewStatus.Stopped;
        #endregion

        #region Properties
        public MainViewStatus Status
        {
            get { return this._Status; }
            set
            {
                this._Status = value;
                this.SetControlButtons();
                if (null != this.StatusChanged)
                {
                    this.StatusChanged(this, new EventArgs());
                }
            }
        }

        public ToolStripButton buttonAddFileTransfer
        {
            get { return this._buttonAddFileTransfer; }
        }

        public ToolStripButton buttonAddDirectoryTransfer
        {
            get { return this._buttonAddDirectoryTransfer; }
        }

        public ToolStripButton buttonPlayAll
        {
            get { return this._buttonPlayAll; }
        }

        public ToolStripButton buttonPauseAll
        {
            get { return this._buttonPauseAll; }
        }

        public ToolStripButton buttonStopAll
        {
            get { return this._buttonStopAll; }
        }

        public ToolStripButton buttonSettings
        {
            get { return this._buttonSettings; }
        }

        public ToolStripButton buttonCheckForUpdates
        {
            get { return this._buttonCheckForUpdates; }
        }

        public DataGridView gridTransfers
        {
            get { return this._gridTransfers; }
        }

        public ToolStripMenuItem menuitemEditTransfer
        {
            get { return this._menuitemEditTransfer; }
        }

        public ToolStripMenuItem menuitemDeleteTransfer
        {
            get { return this._menuitemDeleteTransfer; }
        }

        public NotifyIcon NotifyIcon
        {
            get { return this._NotifyIcon; }
        }

        public ToolStripMenuItem menuitemCheckForUpdates
        {
            get { return this._menuitemCheckForUpdates; }
        }

        public ToolStripMenuItem menuitemPlayAll
        {
            get { return this._menuitemPlayAll; }
        }

        public ToolStripMenuItem menuitemPauseAll
        {
            get { return this._menuitemPauseAll; }
        }

        public ToolStripMenuItem menuitemStopAll
        {
            get { return this._menuitemStopAll; }
        }

        public ToolStripMenuItem menuitemExit
        {
            get { return this._menuitemExit; }
        }

        public ToolStripLabel labelVersion
        {
            get { return this._labelVersion; }
        }
        #endregion

        #region Methods
        public MainView()
        {
            InitializeComponent();

            this._menuitemAuthor.Text = String.Format(
                CultureInfo.CurrentCulture,
                "www.ne0ge0.com // {0}",
                DateTime.Now.Year);
        }

        #region Private Helpers
        public void RemoveRow(
            TransferAgent theInstance)
        {
            // InvokeRequired compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new RemoveRowHandler(this.RemoveRow), 
                    theInstance);
            }
            else
            {
                for (int i = 0; i < this._gridTransfers.Rows.Count; i++)
                {
                    if (this._gridTransfers.Rows[i].Tag.Equals(theInstance))
                    {
                        this._gridTransfers.Rows.RemoveAt(i);
                        break;
                    }
                }

                this._gridTransfers.ClearSelection();

                Application.DoEvents();
            }
        }

        public void SetControlButtons()
        {
            // InvokeRequired compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.InvokeRequired)
            {
                this.Invoke(
                    new SetControlButtonsHandler(SetControlButtons));
            }
            else
            {
                switch (this.Status)
                {
                    case MainViewStatus.Paused:
                        this._buttonPlayAll.Enabled = 
                            this._menuitemPlayAll.Enabled =
                                true;
                        this._buttonPauseAll.Enabled = 
                            this._menuitemPauseAll.Enabled =
                                false;
                        this._buttonStopAll.Enabled = 
                            this._menuitemStopAll.Enabled =
                                true;
                        break;
                    case MainViewStatus.Started:
                        this._buttonPlayAll.Enabled =
                            this._menuitemPlayAll.Enabled =
                                false;
                        this._buttonPauseAll.Enabled =
                            this._menuitemPauseAll.Enabled =
                                true;
                        this._buttonStopAll.Enabled =
                            this._menuitemStopAll.Enabled =
                                true;
                        break;
                    default:
                        this._buttonPlayAll.Enabled =
                            this._menuitemPlayAll.Enabled =
                                true;
                        this._buttonPauseAll.Enabled =
                            this._menuitemPauseAll.Enabled =
                                false;
                        this._buttonStopAll.Enabled =
                            this._menuitemStopAll.Enabled =
                                false;
                        break;
                }
            }
        }
        #endregion
        #endregion

        #region Delegates
        private delegate void SetControlButtonsHandler();
        private delegate void RemoveRowHandler(TransferAgent theInstance);
        #endregion

        #region Events
        public event EventHandler StatusChanged;
        #endregion    
    }
}
